import Header from "./../../components/Header";
import {Row, Col} from "./../../rsuite";
import DownDrop from "./../../components/category";

import {Link} from "react-router";
import {connect} from "react-redux";
import NoData from "./../../components/nodata";
import Search from "./../../components/search";
import Panel from "./../../elements/Panel";
import Pagination from "./../../components/Pagination/";
import Config from "./../../libs/config";
import {Lang} from "./../../libs/";
import {URL, pureUrl, md5Url} from "./../../libs/Url";
import {SUBJECT, PAGE_SUBJECT} from "./../../actions/subject";
import {setArgumentItem,fetch, fetch_remove, enable_delete_btn, selectAllItem, selectItem} from "../../actions";

const News = React.createClass({
    componentDidMount() {
        const {dispatch} = this.props;
        dispatch(fetch({url: URL.NOTICE_CATEGORY_FIND}));
        dispatch(fetch({url: URL.NOTICE_FIND}));
    },
    search(keyword){
        const {dispatch} = this.props;
        dispatch(fetch({url: URL.NOTICE_FIND, keyword}))
    },
    handleSelect(eventKey){
        const {dispatch} = this.props;
        dispatch(fetch({url: URL.NOTICE_FIND, type: eventKey}))
    },
    render(){
        const self = this;
        const {dispatch, user} = this.props;
        const data = self.props[md5Url.NOTICE_FIND] || {},
            data_category = self.props[md5Url.NOTICE_CATEGORY_FIND] || {};
        (!data.list && (data.list = []));
        (!data_category.list && (data_category.list = []));
        return (
            <Panel>
                <Header>通知列表</Header>
                <div className="table-list-head row">
                    <Col xs={6} md={6}>
                        <Link className='btn btn-primary btn-sm' to={Config.MENUS_ABSOLUTE.NOTICES_ADD}>
                            <i className="fa fa-plus"></i> 新建
                        </Link>
                        <button disabled={(self.props[PAGE_SUBJECT.NOTICE].enable_delete_btn == 0 )}
                                onClick={(e)=> {
                                    if (confirm('确认删除？')) {

                                        dispatch(fetch_remove({
                                            url: URL.NOTICE_DELETE,
                                            target: md5Url.NOTICE_FIND,
                                            ids: _.pluck(_.filter(data.list, (item)=> {
                                                return item.checked;
                                            }), 'id').join(',')
                                        }))
                                    }
                                }}
                                className="btn btn-default btn-sm"
                        >删除
                        </button>
                    </Col>
                    <Col xs={6} md={3} mdOffset={3}>
                        <Search submit={this.search}/>
                    </Col>
                </div>
                <Row>
                    <Col xs={12} md={12}>
                        <table className="table table-bordered table-striped general-table">
                            <thead>
                            <tr>
                                <th style={{width: '50px'}}>
                                    <input type="checkbox" onClick={(e)=> {
                                        const self = this;
                                        dispatch(selectAllItem(md5Url.NOTICE_FIND, e.target.checked));
                                        dispatch(enable_delete_btn(PAGE_SUBJECT.NEWS, {
                                            num: (e.target.checked ? data.list.length : 0),
                                            all: true
                                        }));
                                    }}/>
                                </th>
                                <th style={{width: '60px'}}>NO.</th>
                                <th>标题</th>
                                <th>

                                    <DownDrop
                                        label="分类">
                                        {data_category.list.map(function (item, index) {
                                            return <li key={index}>
                                                <a href="javascript:void(0);"
                                                   onClick={()=> {
                                                       console.log(item);
                                                       dispatch(setArgumentItem(PAGE_SUBJECT.NOTICE, item));
                                                       dispatch(fetch({
                                                           url: URL.NOTICE_FIND,
                                                           type: item.type
                                                       }))
                                                   }}>{item.name}</a>
                                            </li>

                                        })}
                                    </DownDrop>
                                </th>
                                <th>发布时间</th>
                                <th style={{width: '120px'}}>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            {
                                data.list.map((item, index)=> {
                                        item.index = index;
                                        item.isOwn = (item.publisher == user.user.userid);
                                        return <tr key={index}>
                                            <td>
                                                {
                                                    item.isOwn ?
                                                        <input type="checkbox" checked={(item.checked ? 'checked' : '')}
                                                               onClick={(e)=> {
                                                                   dispatch(selectItem(md5Url.NOTICE_FIND, {
                                                                       index: index,
                                                                       checked: e.target.checked
                                                                   }));
                                                                   dispatch(enable_delete_btn(PAGE_SUBJECT.NOTICE, {num: (e.target.checked ? 1 : -1)}));
                                                               }
                                                               }/> : ''
                                                }
                                            </td>
                                            <td className="hidden-phone">{index + 1}</td>
                                            <td><Link to={
                                                Config.MENUS_ABSOLUTE.NOTICES_DETAIL.replace(':id', item.id)
                                            }>{item.title}</Link></td>
                                            <td>{item.classify && item.classify.name}</td>
                                            <td>{Moment(item.intime).format('YYYY-MM-DD HH:mm:SS')}</td>
                                            <td>

                                                <Link to={
                                                    Config.MENUS_ABSOLUTE.NOTICES_DETAIL.replace(':id', item.id)
                                                } className="btn btn-default btn-xs" type="button">
                                                    <i className="fa fa-search-plus"></i>
                                                </Link>
                                                {
                                                    item.isOwn ? <button className="btn btn-default btn-xs" type="button">
                                                        <i className="fa fa-pencil"></i>
                                                    </button> : ''
                                                }
                                                {
                                                    item.isOwn ? <button
                                                        className="btn btn-default btn-xs"
                                                        onClick={
                                                            ()=> {
                                                                if (confirm('确认删除？')) {
                                                                    dispatch(fetch_remove({
                                                                        url: URL.NOTICE_DELETE,
                                                                        target: md5Url.NOTICE_FIND,
                                                                        ids: item.id,
                                                                        index: index
                                                                    }));
                                                                    if (item.checked) {
                                                                        dispatch(enable_delete_btn(md5Url.NOTICE_FIND, {num: -1}))
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        type="button">
                                                        <i className="fa fa-times"></i>
                                                    </button> : ''
                                                }

                                            </td>
                                        </tr>
                                    }
                                )
                            }
                            </tbody>
                        </table>
                        {
                            data.list.length ? '' : <NoData />
                        }
                        {
                            (data.list.length ? <Pagination
                                onselect={(num)=> {
                                    dispatch(fetch({
                                        url: URL.NOTICE_FIND,
                                        currentPage: num
                                    }))
                                }}
                                page={data.page || 1}
                                pages={data.pages || 15}
                            /> : '' )
                        }
                    </Col>
                </Row >
            </Panel>
        )
    }
});
function select(state) {
    return {
        [md5Url.NOTICE_FIND]: state.fetchBySubject[md5Url.NOTICE_FIND],
        [PAGE_SUBJECT.NOTICE]: state.pageBySubject[PAGE_SUBJECT.NOTICE],
        [md5Url.NOTICE_CATEGORY_FIND]: state.fetchBySubject[md5Url.NOTICE_CATEGORY_FIND],
        user: state.user
    };
}
export default connect(select)(News)
